﻿using System.Collections.Generic;
using CID.Domain.Development;
using CID.Domain.Infrastructure;
using CID.Domain.Operations;
using Rhino.Mocks;

namespace CID.Domain.Tests.Infrastructure
{
    internal static class StubsBuilder
    {
        public static IParameter BuildParameterStub(ParameterType type = ParameterType.String, string name = null, ITemplate template = null)
        {
            var result = MockRepository.GenerateStub<IParameter>();
            result.Type = type;
            result.Stub(x=>x.Template).Return(template);
            result.Name = name;
            return result;
        }

        public static ITemplate BuildTemplateStub(ITemplateType type = null, string name = null)
        {
            var result = MockRepository.GenerateStub<ITemplate>();
            result.Stub(x=>x.Type).Return(type);
            result.Name = name;
            return result;
        }

        public static ITemplate BuildTemplateStubWithAllParameterTypes(ITemplateType type = null, string name = null)
        {
            var result = BuildTemplateStub(type, name);
            AddAllParameterTypesToTemplateStub(result);
            return result;
        }

        public static IParameterValue BuildParameterValueStub(IParameter parameter = null, IDefinition definition = null)
        {
            var result = MockRepository.GenerateStub<IParameterValue>();
            result.Stub(x => x.Parameter).Return(parameter);
            result.Stub(x => x.Definition).Return(definition);
            return result;
        }

        public static IDefaultParameterValue BuildDefaultParameterValueStub(IParameter parameter = null, IDefinition definition = null)
        {
            var result = MockRepository.GenerateStub<IDefaultParameterValue>();
            result.Stub(x => x.Parameter).Return(parameter);
            result.Stub(x => x.Definition).Return(definition);
            return result;
        }

        public static IEnvironmentParameterValue BuildEnvironmentParameterValueStub(IEnvironment environment = null, IParameter parameter = null, IDefinition definition = null)
        {
            var result = MockRepository.GenerateStub<IEnvironmentParameterValue>();
            result.Stub(x => x.Parameter).Return(parameter);
            result.Stub(x => x.Environment).Return(environment);
            result.Stub(x => x.Definition).Return(definition);
            return result;
        }

        public static IActivationTemplate BuildActivationTemplateStub()
        {
            return MockRepository.GenerateStub<IActivationTemplate>();
        }

        public static IActivationTemplate BuildActivationTemplateStubWithAllParameterTypes()
        {
            var result = BuildActivationTemplateStub();
            AddAllParameterTypesToTemplateStub(result);
            return result;
        }

        public static IDeactivationTemplate BuildDeactivationTemplateStub()
        {
            return MockRepository.GenerateStub<IDeactivationTemplate>();
        }

        public static IDeactivationTemplate BuildDeactivationTemplateStubWithAllParameterTypes()
        {
            var result = BuildDeactivationTemplateStub();
            AddAllParameterTypesToTemplateStub(result);
            return result;
        }

        public static IDeploymentTemplate BuildDeploymentTemplateStub()
        {
            return MockRepository.GenerateStub<IDeploymentTemplate>();
        }

        public static IDeploymentTemplate BuildDeploymentTemplateStubWithAllParameterTypes()
        {
            var result = BuildDeploymentTemplateStub();
            AddAllParameterTypesToTemplateStub(result);
            return result;
        }

        public static ITemplateType BuildTemplateTypeStub()
        {
            return MockRepository.GenerateStub<ITemplateType>();
        }

        public static IDeploymentAgent BuildDeploymentAgentStub()
        {
            var result = MockRepository.GenerateStub<IDeploymentAgent>();
            result.Stub(x => x.Locations).Return(new ILocation[0]);
            return result;
        }

        public static IPackageType BuildPackageTypeStub()
        {
            return MockRepository.GenerateStub<IPackageType>();
        }

        public static IPackage BuildPackageStub(IPackageType type = null, IVersion version = null)
        {
            var result = MockRepository.GenerateStub<IPackage>();
            result.Stub(x => x.Type).Return(type);
            result.Stub(x => x.Version).Return(version);
            return result;
        }

        public static IDeploymentDefinition BuildDeploymentDefinitionStub(IDeploymentTemplate deploymentTemplate = null)
        {
            var result = MockRepository.GenerateStub<IDeploymentDefinition>();
            result.DeploymentTemplate = deploymentTemplate;
            return result;
        }

        public static IActivationDeactivationDefinition BuildActivationDeactivationDefinition(IActivationTemplate activationTemplate = null, IDeactivationTemplate deactivationTemplate = null)
        {
            var result = MockRepository.GenerateStub<IActivationDeactivationDefinition>();
            result.ActivationTemplate = activationTemplate;
            result.DeactivationTemplate = deactivationTemplate;
            return result;
        }

        private static void AddAllParameterTypesToTemplateStub(ITemplate template)
        {
            var parameters = new List<IParameter>();
            parameters.Add(BuildParameterStub(ParameterType.String, template: template));
            parameters.Add(BuildParameterStub(ParameterType.Boolean, template: template));
            parameters.Add(BuildParameterStub(ParameterType.DateTime, template: template));
            parameters.Add(BuildParameterStub(ParameterType.Decimal, template: template));
            parameters.Add(BuildParameterStub(ParameterType.Integer, template: template));
            parameters.Add(BuildParameterStub(ParameterType.UniqueIdentifier, template: template));
            parameters.Add(BuildParameterStub(ParameterType.Xml, template: template));
            template.Stub(x => x.Parameters).Return(parameters);
        }
    }
}
